package dhm.com.dhmshop.view.mine.myshop;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.order.ParamTypeAdapter;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.Bean;
import dhm.com.dhmshop.framework.module.commodity.activity.CommodityDetailsActivity;
import dhm.com.dhmshop.utils.CustomJiaJian;
import dhm.com.dhmshop.utils.SpUtils;
import dhm.com.dhmshop.view.home.OrderdetailActivity;

public class ChangeorderActivity extends BaseActiity implements LoginContract.IView {

    @BindView(R.id.nofa)
    RadioButton nofa;
    @BindView(R.id.fa)
    RadioGroup fa;
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.myaddress)
    TextView myaddress;
    private PopupWindow popWindow;
    private PressenterImpl pressenter;
    private int shop_id;
    private int order_id;
    private String express_num;
    private String uid;

    @Override
    protected int getLayout() {
        return R.layout.activity_changeorder;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter=new PressenterImpl();
        pressenter.attachView(this);

        shop_id = getIntent().getIntExtra("shop_id", 1);
        order_id = getIntent().getIntExtra("order_id",1);
        express_num = getIntent().getStringExtra("express_num");
        uid = SpUtils.getString(this, "uid");
        showPopwindow();
    }

    @Override
    protected void initData() {

    }

    private String express_type="";
    @OnClick({R.id.back, R.id.about, R.id.sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.about:
                setBackgroundAlpha(0.5f);
                popWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.sure:
                int checkedRadioButtonId = fa.getCheckedRadioButtonId();
                Map<String,String> map=new HashMap<>();
                switch (checkedRadioButtonId){
                    case R.id.nofa:
                        map.put("status","1");
                        break;
                    case R.id.yesfa:
                        map.put("status","2");
                        String s = phone.getText().toString();
                        if (s==null||s.equals("")){
                            Toast.makeText(this, "请输入快递单号", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        map.put("express_num",s);
                        break;
                    default:
                        Toast.makeText(this, "请选择订单状态", Toast.LENGTH_SHORT).show();
                        return;
                }

                if (express_type==null||express_type.equals("")){
                    Toast.makeText(this, "请选择快递类型", Toast.LENGTH_SHORT).show();
                    return;
                }
                map.put("token", Constant.TOKEN);
                map.put("uid",uid);
                map.put("shop_id",shop_id+"");
                map.put("order_id",order_id+"");
                map.put("express_type",express_type+"");

                pressenter.sendMessage(ChangeorderActivity.this,Constant.ModifyOrder,map,Bean.class);
                break;
                default:
        }
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof Bean){
            Bean bean= (Bean) data;
            if (bean.getCode()==1){
                if (bean.getMessage().equals("订单状态更新成功")){
                    Toast.makeText(this, bean.getMessage(), Toast.LENGTH_SHORT).show();
                    finish();
                }
            }else {
                Toast.makeText(this, "订单已更新", Toast.LENGTH_SHORT).show();
                finish();
            }
        }

    }

    @Override
    public void fail(String error) {

    }

    private void showPopwindow() {
        View popView = View.inflate(this, R.layout.popup_express, null);
        RadioGroup fa = popView.findViewById(R.id.fa);
        Button sure = popView.findViewById(R.id.sure);

        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (fa.getCheckedRadioButtonId()){
                    case R.id.yunda:
                        myaddress.setText("韵达");
                        express_type="yunda";
                        break;
                    case R.id.zhongtong:
                        myaddress.setText("中通");
                        express_type="zhongtong";
                        break;
                    case R.id.tiantian:
                        myaddress.setText("天天");
                        express_type="tiantian";
                        break;
                    case R.id.shentong:
                        myaddress.setText("申通");
                        express_type="shentong";
                        break;
                    case R.id.shunfeng:
                        myaddress.setText("顺丰");
                        express_type="shunfeng";
                        break;
                    case R.id.youzhengguonei:
                        myaddress.setText("邮政包裹 / 平邮");
                        express_type="youzhengguonei";
                        break;
                    case R.id.huitongkuaidi:
                        myaddress.setText("百世汇通");
                        express_type="huitongkuaidi";
                        break;
                    default:
                        Toast.makeText(ChangeorderActivity.this, "请选择快递类型", Toast.LENGTH_SHORT).show();
                        return;
                }
                popWindow.dismiss();
            }
        });


        popWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popWindow.setAnimationStyle(R.style.AnimBottom);
        popWindow.setFocusable(true);
        popWindow.setTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        popWindow.setOutsideTouchable(true);// 设置允许在外点击消失


        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // popupWindow隐藏时恢复屏幕正常透明度
                setBackgroundAlpha(1.0f);
            }
        });
    }


    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     *            屏幕透明度0.0-1.0 1表示完全不透明
     */
    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }
}
